var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
$(function(){
	var usercode = $("#user-name",parent.parent.document).attr("code");
	var userkey = $("#user-name",parent.parent.document).attr("key");
	var usertype = $("#user-name",parent.parent.document).attr("usertype");
	keyCheck(usercode,usertype,userkey,function(){
		initSmPages();
		window.onresize = function () {
			initSmPages();
		};
		departmentMenu();
		pageBtns();
	});
});
function addClass(){
	var tclass = getFormData();
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
	var obj = {};
	for(var key in tclass){
		if(tclass[key] != "" && tclass[key] != undefined){
			obj[key] = tclass[key];
		}
	}
	obj.schoolcode = schoolcode;
	var data = {
			cmd:"addClass",
			model:"tclass",
			userKey:userKey,
			data:obj,
			remark:"",						
	};
	processBegin("正在保存中···");
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			processEnd("保存成功");
		}else{
			processError("添加班级信息失败,班级号码或者编码重复");
		}
	},null,null);
}
function pageBtns(){
	check("class-classcode",isNull,"null");
	check("class-grade",isNull,"null");
	check("class-class_",isNull,"null");
	$("#class-teacher-name").attr("disabled","disabled");
	$("#class-teacher-name").css("background-color","#E5E5E5");
	$(".save-btns").click(function(){
		if(checkClass() && check2("check-fail")){
			addClass();
		}
	});
	$("#choice-teacher").unbind("click");
	$("#choice-teacher").click(function(){
		teacherChoice(function(){
			$("#teacher-choice-name",parent.parent.document).val("");
			initTeacherTable(1,10000);
			departmentMenu();
			$("#teacher-choice-submit",parent.parent.document).unbind("click");
			$("#teacher-choice-submit",parent.parent.document).click(function(){
				$("#class-teacher-name").val($("#teacher-choice-name",parent.parent.document).val());
				$("#class-teachercode").val($("#teacher-code",parent.parent.document).val());
				$("#tearcher-choice-dialog",parent.parent.document).hide();
				$(".index-dialog-cover",parent.parent.document).hide();
			});
			
		});
	});
}

function getFormData(){
	new setTClass(null,null,null,null,null);
	var tclass = getTClass();
	var obj = {};
	for(var key in tclass){
		if($("#class-"+key)[0]){
			if($("#class-"+key)[0].tagName.toLocaleLowerCase() == "input"){
				obj[key] = $("#class-"+key).val();
			}else{
				obj[key] = $("#class-"+key+" option:selected").val();
			}
		}
	}
	new setTClass(obj.classcode,obj.grade,obj.class_,obj.type,obj.teachercode);
	return getTClass();
}
function checkClass(){
	if($("#class-classcode").val() == ""){
		$("#class-classcode").focus();
		$("#class-classcode-ex").html("不能为空");
		return false; 
	}else if($("#class-grade").val() == ""){
		$("#class-grade").focus();
		$("#class-grade-ex").html("不能为空");
		return false; 
	}else if($("#class-class_").val() == ""){
		$("#class-class_").focus();
		$("#class-class_-ex").html("不能为空");
		return false; 
	}else{
		return true;
	}
}
function initSmPages(){
	var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
	$(".right-content").height(rcH);
	
	var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
	$(".content-data-show").height(cdsH);
}
